package com.yaozheng.vocationaltraining.utils;

import com.yaozheng.vocationaltraining.R;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;


public class JudgeUtils {
	/**
	 * net网络
	 */
	public static final int CMNET = 3;
	/**
	 * wap网络
	 */
	public static final int CMWAP = 2;
	/**
	 * WIFI网络
	 */
	public static final int WIFI = 1;
	/**
	 * 没有网络
	 */
	public static final int NO_NETWORK = -1;

	/**
	 * 判断是否连接网络
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
		} else {
			NetworkInfo[] info = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static String getVersion(Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(),
					0);
			String version = info.versionName.intern();
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 得到圆形带进度条的对话框
	 * 
	 * @param message
	 *            信息
	 * @param context
	 *            当前上下文
	 * @return 返回一个带对话款的进度条
	 */
	public static ProgressDialog getRoundProgressDialog(String message,
			Context context, OnCancelListener cancellistener) {
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// progressDialog.setTitle(this.getString(R.string.alertTitle));
		progressDialog.setMessage(message);
		// progressDialog.setIcon(R.drawable.ico);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		if (cancellistener != null)
			progressDialog.setOnCancelListener(cancellistener);
		return progressDialog;

	}
	/**
	 * 得到长方形带进度条的对话框
	 * 
	 * @param message
	 *            信息
	 * @param context
	 *            当前上下文
	 * @return 返回一个带对话款的进度条
	 */
	public static ProgressDialog getElongatedProgressDialog(String message,
			Context context, OnCancelListener cancellistener) {
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// progressDialog.setTitle(this.getString(R.string.alertTitle));
		progressDialog.setMessage(message);
		// progressDialog.setIcon(R.drawable.ico);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		if (cancellistener != null)
			progressDialog.setOnCancelListener(cancellistener);
		return progressDialog;
	}

	/**
	 * 创建对话框
	 * 
	 * @param reid
	 *            对话框的内容,对应R.string.reid文件的字符串
	 * @param context
	 *            当前上下文
	 * @return 返回一个对话框
	 */
	public static AlertDialog.Builder getBuilder(int messReid, Context context) {

		return getBuilder(context.getString(messReid), context);
	}

	/**
	 * 创建对话框
	 * 
	 * @param reid
	 *            对话框的内容,对应R.string.reid文件的字符串
	 * @param context
	 *            当前上下文
	 * @return 返回一个对话框
	 */
	public static AlertDialog.Builder getBuilder(String messStr, Context context) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.alertTitle);
		builder.setMessage(messStr);
		return builder;
	}

	/**
	 * 得到Toast
	 * 
	 * @param messReid
	 *            消息对应的id
	 * @param context
	 *            当前上下文
	 * @return 返回toast
	 */
	public static Toast getToast(int messReid, Context context) {
		return Toast.makeText(context, messReid, Toast.LENGTH_LONG);
	}

	/**
	 * 获取当前的网络
	 * 
	 * @param context
	 *            当前上下文
	 * @return 状态 -1：没有网络 1：WIFI网络2：wap网络3：net网络
	 */
	public static int getAPNType(Context context) {
		int netType = NO_NETWORK;
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
				netType = CMNET;
			} else {
				netType = CMWAP;
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = WIFI;
		}

		return netType;

	}

	/**
	 * 打开网络连接Activity
	 */
	public static void openNetworkConnectActivity(Context context) {
		Intent mIntent = new Intent("/");
		if (android.os.Build.VERSION.SDK_INT > 10) {
			mIntent = new Intent(
					android.provider.Settings.ACTION_WIRELESS_SETTINGS);
		} else {
			mIntent = new Intent();
			ComponentName component = new ComponentName("com.android.settings",
					"com.android.settings.WirelessSettings");
			mIntent.setComponent(component);
			mIntent.setAction("android.intent.action.VIEW");
		}
		context.startActivity(mIntent);
	}

}
